package com.carbeauty.common.entity;

import com.carbeauty.common.enums.BusinessStatus;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 基础实体类
 * 
 * 包含所有实体的公共字段:
 * - id: 主键ID
 * - createTime: 创建时间
 * - updateTime: 更新时间
 * - status: 业务状态
 * - remark: 备注信息
 * 
 * @author CarBeauty Team
 */
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {
    
    /**
     * 主键ID - 使用自增策略
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Long id;
    
    /**
     * 创建时间 - 自动设置
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
    
    /**
     * 更新时间 - 自动更新
     */
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;
    
    /**
     * 业务状态 - 默认为启用状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private BusinessStatus status = BusinessStatus.ACTIVE;
    
    /**
     * 备注信息
     */
    @Column(name = "remark", length = 500)
    private String remark;
    
    // ==================== 构造方法 ====================
    
    public BaseEntity() {
        this.status = BusinessStatus.ACTIVE;
    }
    
    // ==================== Getter/Setter ====================
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public LocalDateTime getCreateTime() {
        return createTime;
    }
    
    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }
    
    public LocalDateTime getUpdateTime() {
        return updateTime;
    }
    
    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }
    
    public BusinessStatus getStatus() {
        return status;
    }
    
    public void setStatus(BusinessStatus status) {
        this.status = status;
    }
    
    public String getRemark() {
        return remark;
    }
    
    public void setRemark(String remark) {
        this.remark = remark;
    }
    
    // ==================== 业务方法 ====================
    
    /**
     * 检查是否为活跃状态
     */
    public boolean isActive() {
        return BusinessStatus.ACTIVE.equals(this.status);
    }
    
    /**
     * 设置为活跃状态
     */
    public void activate() {
        this.status = BusinessStatus.ACTIVE;
    }
    
    /**
     * 设置为禁用状态
     */
    public void deactivate() {
        this.status = BusinessStatus.INACTIVE;
    }
    
    /**
     * 逻辑删除
     */
    public void delete() {
        this.status = BusinessStatus.DELETED;
    }
    
    /**
     * 检查是否已删除
     */
    public boolean isDeleted() {
        return BusinessStatus.DELETED.equals(this.status);
    }
    
    // ==================== Object方法 ====================
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        BaseEntity that = (BaseEntity) obj;
        return id != null && id.equals(that.id);
    }
    
    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
    
    @Override
    public String toString() {
        return getClass().getSimpleName() + "{" +
                "id=" + id +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", status=" + status +
                '}';
    }
}
